﻿namespace dPet.Domain.Events.Handlers
{
	using dPet.Domain.Models;
	using dPet.Domain.Repositories;
	using Hangerd.Event;

	public class InventoryReducedEventHandler : IDomainEventHandler<InventoryReducedEvent>
	{
		private readonly IInventoryReducingRecordRepository _inventoryReducingRecordRepository;

		public InventoryReducedEventHandler(IInventoryReducingRecordRepository inventoryReducingRecordRepository)
		{
			_inventoryReducingRecordRepository = inventoryReducingRecordRepository;
		}

		public void Handle(InventoryReducedEvent e)
		{
			var productInventory = e.Source as ProductInventory;

			//添加出库记录
			var inventoryReducingRecord = new InventoryReducingRecord(
				productInventory,
				e.Order,
				e.ReduceQuantity);

			_inventoryReducingRecordRepository.Add(inventoryReducingRecord, false);
		}
	}
}
